<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Middleware\AdminMiddleware;
use App\Model\QmGameType;
use App\Service\ToolService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: '/admin/GameType'), Middleware(AdminMiddleware::class)]
class GameTypeController extends BaseController {

    /**
     * 获取分类列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $origin = QmGameType::get();
        $origin = ToolService::buildArrFromObj($origin);
        $list = ToolService::listToTree($origin);

        return $this->buildSuccess([
            'list' => $list
        ]);
    }

    /**
     * 获取游戏分类下拉
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[GetMapping(path: 'getChooseItem')]
    public function getChooseItem(): array {
        $origin = QmGameType::query()->where('status', 1)->get();
        $arr = [];
        foreach ($origin as $value) {
            $value = $value->toArray();
            $value['label'] = $value['title'];
            $value['value'] = $value['id'];
            $arr[] = $value;
        }

        return $this->buildSuccess([
            'list'   => ToolService::listToTree($arr),
            'origin' => ToolService::buildArrByNewKey($arr)
        ]);
    }

    /**
     * 新增分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $fidArr = $this->request->post('fid', '');

        $obj = new QmGameType();
        $obj->title = $this->request->post('title', '');
        $obj->relation = implode(',', array_filter($fidArr));
        $obj->fid = intval(array_pop($fidArr));
        $obj->save();

        return $this->buildSuccess();
    }

    /**
     * 分类状态编辑
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[GetMapping(path: 'changeStatus')]
    public function changeStatus(): array {
        $id = $this->request->query('id');
        $status = $this->request->query('status');

        $res = QmGameType::query()->whereRaw('FIND_IN_SET(?, relation)', [$id])->update([
            'status' => $status
        ]);
        if (!$res) {
            return $this->buildFailed(ErrorCode::DB_SAVE_ERROR);
        }

        return $this->buildSuccess();
    }

    /**
     * 编辑分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[PostMapping(path: 'edit')]
    public function edit(): array {
        $fidArr = $this->request->post('fid', '');
        $obj = QmGameType::find($this->request->post('id'));

        $obj->title = $this->request->post('title', '');
        $obj->relation = implode(',', array_filter($fidArr));
        $obj->fid = intval(array_pop($fidArr));
        $obj->save();

        return $this->buildSuccess();
    }

    /**
     * 删除分类
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[GetMapping(path: 'del')]
    public function del(): array {
        $id = $this->request->query('id');
        if (!$id) {
            return $this->buildFailed(ErrorCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $childNum = QmGameType::query()->where('fid', $id)->count();
        if ($childNum) {
            return $this->buildFailed(ErrorCode::INVALID, '当前分类存在子类别,不可以被删除!');
        }
        QmGameType::destroy($id);

        return $this->buildSuccess();
    }
}